require "Object"
require "ScoreBoard"


--TuZi class
TuZi = Object:new{
	ccatFile = "tuzi/tuzi.ccat",
	idle = "idle",
	run_idle = "run_idle",
	run = "run",
	tiao = "tiao",
	
	destLocation = nil,
	--runSpeed = 90.0,
	bMoving = false,
	bJumping = false,
	--jumpHeight = 50,
	jumpTime = 0.6,
	gravity = CCPoint(0, -550),
	velocity = CCPoint(0, 0),
	world = nil,
	collideNode = nil,
	tuziHeight = 18,
}

function TuZi:onEnter()
	Misc:log("TuZi : onEnter .. " .. Misc:getNodeName(self))
	
	local idleAct = CCATFileCache:sharedCCATFileCache():findAction(self.idle, self.ccatFile)
	self:runAction(idleAct)
	
	self.destLocation = self:getPositionLua()
	
	self.collideNode = Misc:getChildByName(self, "collide")
end


function TuZi:getCollideNode()
	--Misc:log("width = "..self.collideNode:getContentSize().width..", height"..self.collideNode:getContentSize().height)
	return self.collideNode
end

function TuZi:tick(dt)
	
	if self.bJumping then
		local curPos = self:getPositionLua()
		local offset = CCPoint(0,0) --ccpSub(self.destLocation, curPos)
		offset.x = self.destLocation.x - curPos.x
		offset.y = self.destLocation.y - curPos.y
		local delta = CCPoint(0,0) --ccpMult(self.velocity, dt)
		delta.x = self.velocity.x*dt
		delta.y = self.velocity.y*dt
		
		curPos.x = curPos.x + delta.x--ccpAdd(curPos, delta)
		curPos.y = curPos.y + delta.y
		if delta.x * offset.x <= 0 or self.velocity.x * self.gravity.x >= 0 then
			curPos.x = self.destLocation.x
			self.velocity.x = 0
			self.gravity.x = 0
		end
		if curPos.y <= self.tuziHeight then
			curPos.y = self.tuziHeight
		end
		self:setPosition(curPos.x, curPos.y)
		
		self.velocity.x = self.velocity.x + self.gravity.x * dt--ccpAdd(self.velocity, ccpMult(self.gravity, dt))
		self.velocity.y = self.velocity.y + self.gravity.y * dt
		if curPos.y <= self.tuziHeight then
			self:stopAllActions()
			local run_idleAct = CCATFileCache:sharedCCATFileCache():findAction(self.run_idle, self.ccatFile)
			self:runAction(run_idleAct)
			self.bJumping = false
			local moveAct = CCMoveTo:create(0.4, CCPoint(0,0))
			local sineOutAct = CCEaseSineIn:create(moveAct)
			self.world:runAction(sineOutAct)
			
			if self.world.lingdangFactory.collideCount >= 1 then
				ScoreBoard:create("score_board.cc2d", self.world)
			end
		end
		return
	end
	if self.bMoving then
		local curPos = self:getPositionLua()
		local offset = ccpSub(self.destLocation, curPos)
		local delta = ccpMult(self.velocity, dt)
		self.velocity.y = 0
		curPos = ccpAdd(curPos, delta)
		if ccpLength(delta) > ccpLength(offset) then
			self:setPosition(self.destLocation.x, self.tuziHeight)
			self:stopAllActions()
			local run_idleAct = CCATFileCache:sharedCCATFileCache():findAction(self.run_idle, self.ccatFile)
			self:runAction(run_idleAct)
			self.bMoving = false
			return
		end
		self:setPosition(curPos.x, self.tuziHeight)
	end
end

function TuZi:onExit()
	
end

function TuZi:tiaoYue(pos)
	
	--if not self.bJumping then
		self:stopAllActions()
		local runAct = CCATFileCache:sharedCCATFileCache():findAction(self.tiao, self.ccatFile)
		self:runAction(runAct)
	--end
	
	self.bJumping = true
	self.bMoving = false
	
	self.gravity.x = -(pos.x - self:getPositionLua().x)*2.0/self.jumpTime/self.jumpTime
	pos.y = self:getPositionLua().y + (-self.gravity.y)*self.jumpTime*self.jumpTime/2.0
	self.destLocation = pos
	self.velocity.x = (-self.gravity.x)*self.jumpTime --(pos.x - self:getPositionLua().x)/(self.jumpTime*0.5)
	self.velocity.y = (-self.gravity.y)*self.jumpTime
	--local move = CCEaseSineOut:create( CCMoveTo:create(0.8, self.destLocation) )
	--self:runAction(move)
	
	if pos.x - self:getPositionLua().x < 0 then
		self:setFlipX(true)
	else
		self:setFlipX(false)
	end
end

function TuZi:moveTo(pos)
	if pos.x - self:getPositionLua().x < 0 then
		self:setFlipX(true)
	else
		self:setFlipX(false)
	end
	
	if self.bJumping then
		self.destLocation = pos
		self.gravity.x = -(pos.x - self:getPositionLua().x)*2.0/self.jumpTime/self.jumpTime
		self.velocity.x = (-self.gravity.x)*self.jumpTime 
		return;
	end
	
	if not self.bMoving then
		self:stopAllActions()
		local runAct = CCATFileCache:sharedCCATFileCache():findAction(self.run, self.ccatFile)
		self:runAction(runAct)
	end
	
	self.bMoving = true
	self.bJumping = false
	pos.y = self:getPositionLua().y
	self.destLocation = pos
	if pos.x - self:getPositionLua().x == 0 then
		return;
	end
	self.velocity.x = (pos.x - self:getPositionLua().x)/math.abs((pos.x - self:getPositionLua().x)) * 160
	
end

function TuZi:create(tuziFile, world, tuzi)
	local newTuZi = tuzi or TuZi:new()
	
	newTuZi.world = world
	
	local cc2dNode = CC2DFileCache:sharedCC2DFileCache():addCC2DNode(tuziFile)
	local ccTuZi = tolua.cast(cc2dNode:createCCNode(world), "CCSprite")
	
	Misc:setNodeName(ccTuZi, "tuzi")
	
	tolua.setpeer(ccTuZi, newTuZi)
	
	local function onEnterOrExit(eventType)
		if eventType == kCCNodeOnExit then
			ccTuZi:onExit()
		else
			ccTuZi:onEnter()
		end
	end
	
	ccTuZi:registerScriptHandler(onEnterOrExit)
	
	if ccTuZi:isRunning() then
		ccTuZi:onEnter()
	end
	return ccTuZi
end